<!-- <template xmlns="">
  <el-popover
    placement="right"
    width="400"
    trigger="click">
    <el-upload
      class="upload-demo"
      drag
      :action="actionUrl"
      :headers="headers"
      :limit="1"
      :file-list="fileList"
      :data="uploadData"
      :before-upload="beforeUpload"
      :on-success="handleUploadSuccess"
      multiple>
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div class="el-upload__tip" slot="tip"></div>
    </el-upload>
    <el-button slot="reference" @click="upload()">导入</el-button>
    
  </el-popover>      
</template>

<script>
import {queryfiles,putfile,deletefile} from "@/api/baiduapi";
import { getAccessToken } from '@/utils/auth'

export const uploadStore = defineStore('upload', {
  state: () => ({
    multiFileList: [],
    processNumber: 3,
    promise: []
  })
})
export default {
  name: "Login",
  components: { },
  props:{
    menulist:{
      type:Array,
      default:[]
    }
  },
  data() {
    return {
      fileList: [],
      listdata:{},
      actionUrl: process.env.VUE_APP_BASE_API + '/admin-api/qa/files/upload',
      headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部
      uploadData: {
        file: '',
        name: this.filename
      },
    };
  },
  created() {

  },
  mounted () {
    
  },
  methods: {
    beforeUpload(raw) {
      addUploadFile(raw)
      return false
    },
    addUploadFile(raw) {
      const upload = uploadStore()
      const uploadProgress = {
          progress: 0,
          file_id: '',
          file_name: raw.name,
          percent: [],
          speed: '0 MB',
          file_size: raw.size,
          upload_size: 0,
          upload_time: new Date()
      }
      // status上传状态 0 队列，1 上传中，2 上传成功 ， 3 取消上传
      // failTryCount 失败上传次数, 没上传一次，自动减去已，当为0的时候，停止上传
      upload.multiFileList.push({file: raw, progress: uploadProgress, status: 0, failTryCount: 3})
      multiUpload()
    },

    multiUpload() {
      const upload = uploadStore()
      const readFileList = []
      upload.multiFileList.forEach(res => {
          if (res.status === 0) {
              readFileList.push(res)
          }
      })
      if (readFileList.length > 0) {
          multiRun(upload, readFileList.slice(0, upload.processNumber), uploadAsync)
      }
    },
    multiRun(upload, keyList, func) {
      const processNumber = upload.processNumber
      const promise = upload.promise
      for (let i = 0; i < processNumber - promise.length; i++) {
          promise.push(Promise.resolve())
      }
      let reduceNumber = promise.length - processNumber
      if (reduceNumber > 0) {
          upload.promise = promise.slice(0, reduceNumber)
      }
      for (let j = 0; j < keyList.length; j += processNumber) {
          for (let i = 0; i < processNumber; i++) {
              if (i + j < keyList.length) {
                  promise[(j + i) % processNumber] = promise[(j + i) % processNumber].then(() => func(keyList[i + j])).catch(({
                                                                                                                                  fileInfo,
                                                                                                                                  err
                                                                                                                              }) => {
                      if (fileInfo.status === 3) {
                          console.log(fileInfo.file.name, '取消上传')
                      } else {
                          fileInfo.status = 0
                          fileInfo.failTryCount -= 1
                          if (fileInfo.failTryCount < 1) {
                              ElMessage.error(`${fileInfo.file.name} 超过最大重试次数，停止上传`)
                          } else {
                              ElMessage.error(`${fileInfo.file.name} 上传失败，正在重试`)
                              console.log(fileInfo.file.name, err)
                              multiUpload()
                          }
                      }
                  })
              }
          }
      }
    },
    uploadAsync(fileInfo) {
    const progress = fileInfo.progress
    const file = fileInfo.file
    return new Promise((resolve, reject) => {
        progress.file_name = file.name
        progress.file_size = file.size
        if (fileInfo.status === 0) {
            fileInfo.status = 1
        } else {
            return resolve()
        }
        progress.progress = 10
        getUploadSid().then(async res => {
            // ElMessage.info(fileName + ' 文件读取中')
            progress.speed = '文件读取中'
            let hash = await PreHash(file, progress)
            let fileHashInfo = {
                sid: res.data.sid,
                file_name: progress.file_name,
                file_size: progress.file_size,
                pre_hash: hash
            }
            progress.progress = 20
            checkPreHash(fileHashInfo).then(async pRes => {
                if (pRes.data.check_status === true) {
                    // 秒传逻辑
                    progress.progress = 30
                    const md5Code = pRes.data.md5_token
                    progress.speed = '文件校验中'
                    // ElMessage.info(fileInfo.file_name + ' 秒传检测中')
                    let hash = await ContentHash(file, md5Code, progress)
                    fileHashInfo.proof_code = hash.proofCode
                    fileHashInfo.content_hash = hash.conHash
                    checkContentHash(fileHashInfo).then(async cRes => {
                        if (cRes.data.check_status === true) {
                            progress.progress = 100
                            progress.upload_size = progress.file_size
                            progress.speed = '秒传成功'
                            // ElMessage.success(fileName + ' 上传成功')
                            fileInfo.status = 2
                            fileInfo.upload_time = new Date()
                            multiUpload()
                            resolve()
                        } else {
                            return await ChunkedUpload(fileInfo, fileHashInfo, cRes.data.upload_extra, cRes.data.part_info_list, () => {
                                fileInfo.status = 2
                                fileInfo.upload_time = new Date()
                                multiUpload()
                                resolve()
                            }, (err) => {
                                reject({fileInfo, err})
                            })
                        }
                    }).catch((err) => {
                        reject({fileInfo, err})
                    })
                } else {
                    return await ChunkedUpload(fileInfo, fileHashInfo, pRes.data.upload_extra, pRes.data.part_info_list, () => {
                        fileInfo.status = 2
                        fileInfo.upload_time = new Date()
                        multiUpload()
                        resolve()
                    }, (err) => {
                        reject({fileInfo, err})
                    })
                }
            }).catch((err) => {
                reject({fileInfo, err})
            })
        }).catch((err) => {
            reject({fileInfo, err})
        })
    })
},
















    updateclose(){
      this.flag3 = !this.flag3
    },
    beforeImageUpload(file){
      const isType = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
      if (!isType) {
        this.$message.error('上传文件格式不对!');
        return false;
      }
      const isLt = file.size / 1024 / 1024 < 2;
      if (!isLt) {
        this.$message.error('上传文件大小不能超过 2M!');
        return false;
      }
      this.uploadData.name = file.name;
      this.uploadData.file = file;
      setTimeout(() => {
        queryfiles().then(res => {
          const tenantId = res.data;
          this.menulist1=tenantId.list
          // console.log('menulist1',this.menulist1)
          this.filename = this.menulist1[0].filename
          this.handleSelect(1,this.menulist1[0])
        });
      }, 1000);

      return true;
    },
    handleUploadSuccess(response, file, fileList) {
      this.loading=true
      console.log('response',response)
      if (response.code !== 0) {
        this.$message.error('上传出错：' + response.msg)
        return false;
      }

      // 清空上传时的各种数据
      this.fileList = []
      this.uploadData.title = ''
      this.uploadData.introduction = ''

      // 上传好的文件，本质是个素材，所以可以进行选中
      let item = response.data
      this.selectMaterial(item)
      queryfiles().then(res => {
        const tenantId = res.data;
        this.menulist1=tenantId.list
        // console.log('menulist1',this.menulist1)
        this.filename = this.menulist1[0].filename
        this.handleSelect(1,this.menulist1[0])
      });

    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/assets/styles/login.scss";

</style> -->
